<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\index\model\TestModel;
use app\index\model\MarkModel;
use app\index\model\StudentModel;
use app\index\model\TestasModel;
use app\index\model\TestaModel;
use app\index\model\TestbsModel;
use app\index\model\TestbModel;
use app\index\model\TestcsModel;
use app\index\model\TestcModel;
use app\index\model\TestdsModel;
use app\index\model\TestdModel;
use app\index\model\TestesModel;
use app\index\model\TesteModel;
use app\index\model\User;

class Main extends Controller
{

    function __construct() {

        parent::__construct();

        $isLogin = Session::get('isLogin');
        if (1 != $isLogin){
            return $this->redirect('/index/index');
        }
    }

    public function index()
    {
        $id = Session::get('nid');
        $student = StudentModel::get($id);
        $data['student']  =  $student;

        $test = new TestModel();
        $tests=($test->getTest());
        $data['tests']  =  $tests;

//        $list = new MarkModel();
//        $lists = $list::get(['number'=>$tests->number,'admin'=>$tests->admin]);
//        $data['lists']  =  $lists;
        return $this->fetch('/main/index',$data);
    }

    public function infoa()
    {
        $admin = Session::get('nid');

        $number  = Session::get('number');
        $tes =  TestModel::get(['number'=>$number]);
        $data['tes']  =  $tes;

        $timea = MarkModel::get(['number'=>$number,'admin'=>$admin]);
        $data['timea']  =  $timea;
        $H = floor(((($timea->timea)+($tes->times)*60)-(date('H')*3600 +date('i')*60 +date('s')))/60);
        $data['H']  =  $H;
        $i= (((($timea['timea'])+($tes['times'])*60)-(date('H')*3600 +date('i')*60 +date('s')))%60);
        $data['i']  =  $i;

        $nids = Session::get('nid');
        $student = StudentModel::all(['id'=>$nids]);
        $data['student']  =  $student;

        $ndr = Request::instance()->get('nid');
        $ndt = Request::instance()->get('n');
        if ($ndr==null){
            if($ndt==null){
                $nd=1;
            }else {
                $nd = $ndt;
            }
        } else{
            $nd = $ndr;
        }
        $nids = TestaModel::get(['number'=>$number,'nid'=>$nd]);
        $data['nids']  =  $nids;


        $data['number']  =  $number;

        if ($nids=="" ){
            $A='yellow';
            $data['A']  =  $A;
            $B='main';
            $data['B']  =  $B;
            $C='main';
            $data['C']  =  $C;
            $D='main';
            $data['D']  =  $D;
            $E='main';
            $data['E']  =  $E;
            return $this->fetch('/main/indexs',$data);
        }else{
            $id = $nids->id;
            $nidsss = TestasModel::get(['number'=>$number,'nid'=>$id,'admin'=>$admin]);
            $data['nidsss']  =  $nidsss;
            return $this->fetch('/main/infoa',$data);
        }
    }

    public function infoas()
    {
        $admin = Session::get('nid');
        $nid = Session::get('nid');
        $student = StudentModel::all(['id'=>$nid]);;
        $data['student']  =  $student;

        $id = Session::get('NDD');
        $number = Session::get('number');
        $data['number'] = $number;
        $testss=TestaModel::all(['number'=>$number]);
        $data['testss']  =  $testss;

        $tes =  TestModel::get(['number'=>$number]);
        $data['tes']  =  $tes;

        $testa = TestaModel::get(['number'=>$number,'nid'=>$id]);
        if($testa=='' ){
            $testa = TestaModel::get(['number'=>$number,'nid'=>$id-1]);
            $data['testa']  =  $testa;
        }
        $data['testa']  =  $testa;

        $timea = MarkModel::get(['number'=>$number,'admin'=>$nid]);
        $data['timea']  =  $timea;

        $H = floor(((($timea->timea)+($tes->times)*60)-(date('H')*3600 +date('i')*60 +date('s')))/60);
        $data['H']  =  $H;
        $i= (((($timea['timea'])+($tes['times'])*60)-(date('H')*3600 +date('i')*60 +date('s')))%60);
        $data['i']  =  $i;

        $nida = $testa->id;
        $nidas = TestasModel::get(['number'=>$number,'nid'=>$nida,'admin'=>$admin]);
        $data['nidas']  =  $nidas;

        return $this->fetch('/main/infoas',$data);
    }


    public function momo()
    {
        //时间模块
        $nid = Session::get('nid');
        $number = Session::get('number');
        $data['number']  =  $number;
        $timea = MarkModel::get(['number'=>$number,'admin'=>$nid]);
        $data['timea']  =  $timea;
        $tes =  TestModel::get(['number'=>$number]);
        $data['tes']  =  $tes;
        $H = floor(((($timea->timea)+($tes->times)*60)-(date('H')*3600 +date('i')*60 +date('s')))/60);
        $data['H']  =  $H;
        $i= (((($timea['timea'])+($tes['times'])*60)-(date('H')*3600 +date('i')*60 +date('s')))%60);
        $data['i']  =  $i;

        //单选选题号模块
        $testas=TestaModel::all(['number'=>$number]);
        if($testas==null){
            $AS="本次考试无该类型试题";
            $data['AS']  =  $AS;
        } else {
            $AS="";
            $data['AS']  =  $AS;
        }
        $data['testas']  =  $testas;
        //已做题号模块
        $testa= TestasModel::all(['number'=>$number,'admin'=>$nid]);
        if ($testa== null){
            $testao="该类型题尚未开始作答";
            $data['testao']  =  $testao;
        } else {
            $testao="";
            $data['testao']  =  $testao;
        }
        $data['testa']  =  $testa;

        //多选题号模块
        $testbs=TestbModel::all(['number'=>$number]);
        if($testbs==null){
            $BS="本次考试无该类型试题";
            $data['BS']  =  $BS;
        } else {
            $BS="";
            $data['BS']  =  $BS;
        }
        $data['testbs']  =  $testbs;
        //已做题号模块
        $testb= TestbsModel::all(['number'=>$number,'admin'=>$nid]);
        if ($testb== null){
            $testbo="该类型题尚未开始作答";
            $data['testbo']  =  $testbo;
        } else {
            $testbo="";
            $data['testbo']  =  $testbo;
        }
        $data['testb']  =  $testb;

        //判断题号模块
        $testcs=TestcModel::all(['number'=>$number]);
        if($testcs==null){
            $CS="本次考试无该类型试题";
            $data['CS']  =  $CS;
        } else {
            $CS="";
            $data['CS']  =  $CS;
        }
        $data['testcs']  =  $testcs;
        //已做题号模块
        $testc= TestcsModel::all(['number'=>$number,'admin'=>$nid]);
        if ($testc== null){
            $testco="该类型题尚未开始作答";
            $data['testco']  =  $testco;
        } else {
            $testco="";
            $data['testco']  =  $testco;
        }
        $data['testc']  =  $testc;

        //填空题号模块
        $testds=TestdModel::all(['number'=>$number]);
        if($testds==null){
            $DS="本次考试无该类型试题";
            $data['DS']  =  $DS;
        } else {
            $DS="";
            $data['DS']  =  $DS;
        }
        $data['testds']  =  $testds;
        //已做题号模块
        $testd= TestdsModel::all(['number'=>$number,'admin'=>$nid]);
        if ($testd== null){
            $testdo="该类型题尚未开始作答";
            $data['testdo']  =  $testdo;
        } else {
            $testdo="";
            $data['testdo']  =  $testdo;
        }
        $data['testd']  =  $testd;

        //简答题号模块
        $testes=TesteModel::all(['number'=>$number]);
        if($testes==null){
            $ES="本次考试无该类型试题";
            $data['ES']  =  $ES;
        } else {
            $ES="";
            $data['ES']  =  $ES;
        }
        $data['testes']  =  $testes;
        //已做题号模块
        $teste= TestesModel::all(['number'=>$number,'admin'=>$nid]);
        if ($teste== null){
            $testeo="该类型题尚未开始作答";
            $data['testeo']  =  $testeo;
        } else {
            $testeo="";
            $data['testeo']  =  $testeo;
        }
        $data['teste']  =  $teste;

        return $this->fetch('/momo/index',$data);
    }

    public function doAdd()
    {
        $nid = Request::instance()->post('nid');
        $nids = Request::instance()->post('nids');
        Session::set('NDD',$nids+1);
        $number = Request::instance()->post('number');
        $answer = Request::instance()->post('answer');
        $admin = Request::instance()->post('admin');
        $answers = Request::instance()->post('answers');
        if($answer==$answers){
            $score="1";
        } else{
            $score="0";
        }
        $id = Request::instance()->post('id');
        $teatas = new TestasModel;
        // var_dump($teatas);
        if ( $id =="") {
            $teatass = [
                ['nid' => $nid,'n' => $nids, 'number' => $number, 'answer' => $answer, 'admin' => $admin, 'answers' => $answers, 'score' => $score]
            ];
            var_dump($teatass);
            $teatas->saveAll($teatass);
        } else {
            $teatas->where('id', $id)
                ->update( ['nid' => $nid,'n' => $nids, 'number' => $number, 'answer' => $answer, 'admin' => $admin, 'answers' => $answers, 'score' => $score]);
            var_dump($teatas);
        }
        return $this->redirect('/index/main/infoas');
    }
}